import React, { Component } from 'react';
import { Spin, Button, Tabs, Col } from 'antd';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { AuthCode, AuthCodeContext } from '@xforceplus/authcode';
import { LanguagePackage } from '@/utils/createContext';
import { getToSendList, getCountWait, getCountParcel, getParcel, searchFilter, getUsersourceapiUrls } from './module/action';
//------------------------------------------------
import ToSend from './components/tosend/ToSend';
import SendIn from './components/sendin/SendIn';
import HasSend from './components/hassend/HasSend';
import ErrSend from './components/errsend/ErrSend';
//------------------------------------------------
import './index.scss';
const TabPane = Tabs.TabPane;

class Logistics extends Component {
  state = {
    parcelFlag: 1,
    businessLgtStatus: 3
  }
  componentDidMount() {
    this.props.getUsersourceapiUrls()
    this.props.getCountWait()
    this.props.getCountParcel()
    this.props.getToSendList()
  }

  changeParcelStatus = (parcelFlag, businessLgtStatus) => {//[(收寄件人)、(寄送中-异常-已签收)]状态
    this.setState({
      parcelFlag: parcelFlag ? parcelFlag : 1,
      businessLgtStatus: businessLgtStatus ? businessLgtStatus : 3
    })
  }


  switchTabPane = (status) => {
    const { orderParam, logisticsRequestParam } = this.props
    const param = logisticsRequestParam
    param.parcelFlag = 1
    const pageParam = {
      pageIndex: 1,
      pageRowCount: 10
    }
    switch (status) {
      case '1':
        this.props.getToSendList()
        this.props.getCountWait()
        this.props.searchFilter(false)
        break;
      case '2':
        param.businessLgtStatus = 3,
          this.changeParcelStatus(1, 3)
        this.props.getParcel({
          logisticsParcelRequestParam: param,
          pageParam: pageParam,
          orderParam: orderParam
        })
        this.props.getCountParcel()
        this.props.searchFilter(true)
        break;
      case '3':
        this.changeParcelStatus(1, 4)
        param.businessLgtStatus = 4,
          this.props.getParcel({
            logisticsParcelRequestParam: param,
            pageParam: pageParam,
            orderParam: orderParam
          })
        this.props.getCountParcel()
        this.props.searchFilter(true)
        break;
      case '4':
        this.changeParcelStatus(1, 5)
        param.businessLgtStatus = 5,
          this.props.getParcel({
            logisticsParcelRequestParam: param,
            pageParam: pageParam,
            orderParam: orderParam
          })
        this.props.getCountParcel()
        this.props.searchFilter(true)
        break;
    }
  }

  render() {
    const { loading, countWait, countParcel, toSendList, userReSource } = this.props;
    const { parcelFlag, businessLgtStatus } = this.state
    const parcelStatus = { parcelFlag: parcelFlag, businessLgtStatus: businessLgtStatus }
    return (
      <Spin
        size="large"
        spinning={loading}
        tip="Loading...">
        <div className="logist-tab">
          <Tabs defaultActiveKey="1" onChange={this.switchTabPane}>
            <TabPane tab={<div>待寄送<span>({countWait ? countWait.waitSend : '-'})</span></div>} key="1"> <ToSend /> </TabPane>
            <TabPane tab={<div>寄送中<span>({countParcel ? countParcel.sendingNum : '-'})</span></div>} key="2"> <SendIn changeParcelStatus={this.changeParcelStatus} parcelStatus={parcelStatus} /></TabPane>
            <TabPane tab={<div>异常<span className={countParcel.exceptionNum == '0' ? '' : 'error'}>({countParcel ? countParcel.exceptionNum : '-'})</span></div>} key="3"> <ErrSend changeParcelStatus={this.changeParcelStatus} parcelStatus={parcelStatus} /></TabPane>
            <TabPane tab={<div>已签收<span>({countParcel ? countParcel.sendedNum : '-'})</span></div>} key="4"> <HasSend changeParcelStatus={this.changeParcelStatus} parcelStatus={parcelStatus} /></TabPane>
            {/* <AuthCodeContext.Provider value={{ "authCodes": userReSource }}>
              <AuthCode auth_code={'X00501000'}>
                <TabPane tab={<div>待寄送<span>({countWait ? countWait.waitSend : '-'})</span></div>} key="1"> <ToSend /> </TabPane>
              </AuthCode>
              <AuthCode auth_code={'X00502000'}>
                <TabPane tab={<div>寄送中<span>({countParcel ? countParcel.sendingNum : '-'})</span></div>} key="2"> <SendIn changeParcelStatus={this.changeParcelStatus} parcelStatus={parcelStatus} /></TabPane>
              </AuthCode>
              <AuthCode auth_code={'X00503000'}>
                <TabPane tab={<div>异常<span className={countParcel.exceptionNum == '0' ? '' : 'error'}>({countParcel ? countParcel.exceptionNum : '-'})</span></div>} key="3"> <ErrSend changeParcelStatus={this.changeParcelStatus} parcelStatus={parcelStatus} /></TabPane>
              </AuthCode>
              <AuthCode auth_code={'X00504000'}>
                <TabPane tab={<div>已签收<span>({countParcel ? countParcel.sendedNum : '-'})</span></div>} key="4"> <HasSend changeParcelStatus={this.changeParcelStatus} parcelStatus={parcelStatus} /></TabPane>
              </AuthCode>
            </AuthCodeContext.Provider> */}
          </Tabs>
        </div>
      </Spin>
    );
  }
}

function mapStateToProps(state) {
  const { loading, countWait, countParcel, toSendList, logisticsRequestParam, orderParam, userReSource } = state.logistics;
  return {
    loading: loading,
    countWait,
    countParcel,
    toSendList,
    logisticsRequestParam,//筛选条件
    orderParam,//排序
    userReSource,//用户资源码
  };
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({
    getToSendList,
    getCountWait,//待寄送总数
    getCountParcel,//寄送中，异常、已签收
    getParcel,//寄送中，异常，已签收 包裹总数
    searchFilter,//筛选条件切换
    getUsersourceapiUrls,//获取用户资源吗
  }, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(Logistics);
